/*
 * Copyright (c) 2015 张涛.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.rzhd.coursepatriarch.common.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

import com.rzhd.coursepatriarch.common.utils.EmojiUtils;
import com.rzhd.coursepatriarch.common.utils.ToastUtils;

/**
 * 自定义文本输入框，增加清空按钮
 */
public class CustomEditText extends AppCompatEditText {

    private Drawable mLeft, mTop, mRight, mBottom;

    private Rect mBounds;
    private TextView totalNumberOfWords;
    /**
     * 最大字数
     */
    private int maxCount;

    /**
     * 输入表情前的光标位置
     */
    private int cursorPos;

    /**
     * 输入表情前EditText中的文本
     */
    private String inputAfterText;

    /**
     * 是否重置了EditText的内容
     */
    private boolean resetText;

    public CustomEditText(Context context) {
        super(context);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setDrawable();
        // 增加文本监听器.
        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                setLimitExpression(s, start, before, count);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (!resetText) {
                    cursorPos = getSelectionEnd();
                    // 这里用s.toString()而不直接用s是因为如果用s，
                    // 那么，inputAfterText和s在内存中指向的是同一个地址，s改变了，
                    // inputAfterText也就改变了，那么表情过滤就失败了
                    inputAfterText = s.toString();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                setDrawable();
                if (totalNumberOfWords != null) {
                    totalNumberOfWords.setText(new StringBuilder()
                            .append(s.length())
                            .append("/")
                            .append(maxCount).toString());
                }
            }
        });
    }

    /**
     * 限制表情
     *
     * @param s
     */
    private void setLimitExpression(CharSequence s, int start, int before, int count) {
        if (!resetText) {
            // 表情符号的字符长度最小为2
            if (count >= 2) {
                CharSequence input = s.subSequence(cursorPos, cursorPos
                        + count);
                if (EmojiUtils.containsEmoji(input.toString())) {
                    resetText = true;
                    //暂不支持输入Emoji表情符号
                    ToastUtils.shortToast("暂不支持输入表情符号");// 是表情符号就将文本还原为输入表情符号之前的内容
                    setText(inputAfterText);
                    CharSequence text = getText();
                    if (text instanceof Spannable) {
                        Spannable spanText = (Spannable) text;
                        Selection.setSelection(spanText, text.length());
                    }
                }
            }
        } else {
            resetText = false;
        }
    }


    // 输入框右边的图标显示控制
    private void setDrawable() {
        if (length() == 0 || !isFocused()) {
            setCompoundDrawables(mLeft, mTop, null, mBottom);
        } else {
            setCompoundDrawables(mLeft, mTop, mRight, mBottom);
        }
    }

    @Override
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        if (mLeft == null) {
            this.mLeft = left;
        }
        if (mTop == null) {
            this.mTop = top;
        }
        if (mRight == null) {
            this.mRight = right;
        }
        if (mBottom == null) {
            this.mBottom = bottom;
        }
        super.setCompoundDrawables(left, top, right, bottom);
    }

    // 输入事件处理
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (mRight != null && event.getAction() == MotionEvent.ACTION_UP) {
            this.mBounds = mRight.getBounds();
            mRight.getIntrinsicWidth();
            int eventX = (int) event.getX();
            int width = mBounds.width();
            int right = getRight();
            int left = getLeft();
            if (eventX > (right - width * 2 - left)) {
                setText("");
                event.setAction(MotionEvent.ACTION_CANCEL);
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        this.mLeft = null;
        this.mTop = null;
        this.mRight = null;
        this.mBottom = null;
        this.mBounds = null;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        setDrawable();
    }

    /**
     * 设置字数
     *
     * @param totalNumberOfWords
     * @param maxCount
     */
    public void setTotalNumberOfWords(TextView totalNumberOfWords, int maxCount) {
        this.totalNumberOfWords = totalNumberOfWords;
        this.maxCount = maxCount;
    }
}